package com.example.liwushuo;

import java.io.File;

import com.example.liwushuo.utils.FileUtils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class BaseApp extends Application {

	private static BaseApp app;
	private static BitmapUtils bitmapUtils;

	private ImageLoaderConfiguration builder;

	@Override
	public void onCreate() {
		super.onCreate();
		app = this;
		getBitmapGlobalConfig();
		getBitmapUtilsMethod();
		initImageLoaderConfig();
		ImageLoader.getInstance().init(builder);
	}

	public BaseApp getApp() {
		return app;
	}

	// 全局配置BitmapUtils
	private void getBitmapGlobalConfig() {
		// 第一个参数是上下文 ， 第二个参数是保存的路径
		String diskCachePath = FileUtils.getImageCoche();
		// 设置图片保存的路径
		BitmapGlobalConfig config = BitmapGlobalConfig.getInstance(this,
				diskCachePath);
		// 设置线程池中线程的个数
		config.setThreadPoolSize(4);

	}

	private void getBitmapUtilsMethod() {

		bitmapUtils = new BitmapUtils(this);
		// 设置图片下载时的动画
		// bitmapUtils.configDefaultImageLoadAnimation(animation);
		// 设置图片下载是的图片
		//bitmapUtils.configDefaultLoadingImage(R.drawable.placeholder);
		// 设置图片下载错误是显示的图片
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.placeholder);

	}

	// 初始化ImageLoader参数

	private void initImageLoaderConfig() {

		File cacheDir = StorageUtils.getIndividualCacheDirectory(this,
				"lws/image"); // 保存图片的位置
		// 显示图片的配置
		builder = new ImageLoaderConfiguration.Builder(this).threadPoolSize(5) // 设置线程池的大小
				.memoryCacheSize(2 * 1024 * 1024)// 设置内存的大小
				.diskCache(new UnlimitedDiskCache(cacheDir)) // 图片保存的位置
				.build();
	}

	public static BitmapUtils getBitmapUtils() {
		return bitmapUtils;
	}

	public static void setBitmapUtils(BitmapUtils bitmapUtils) {
		BaseApp.bitmapUtils = bitmapUtils;
	}

	public static boolean isNetworkConnected(Context context) {
		if (context != null) {

			ConnectivityManager manager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manager.getActiveNetworkInfo();

			if (info != null) {
				return info.isAvailable();
			}
		}
		return false;
	}

	public static boolean isNetWif(Context context) {
		if (context != null) {
			ConnectivityManager manager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manager.getNetworkInfo(manager.TYPE_WIFI);

			if (info != null) {
				return info.isAvailable();
			}
		}
		return false;
	}

	// 获取HttpUtils

	public static HttpUtils getHttpUtils() {
		return new HttpUtils();
	}
	
	public static void getToast(String message){
		
		Toast.makeText(app, message, 1).show();
	}
	
}
